package com.jn.erp.web.api.common.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.common.entity.TLabel;
import com.jn.erp.web.api.common.mapper.TLabelMapper;
import com.jn.erp.web.api.common.service.TLabelService;
import com.jn.erp.web.api.course.mapper.TCourseOrderConfigMapper;
import com.jn.erp.web.api.student.mapper.StudentClassMapper;
import com.jn.erp.web.api.student.mapper.StudentCourseMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-09-03
 */
@Service
public class TLabelServiceImpl extends ServiceImpl<TLabelMapper, TLabel> implements TLabelService {

    @Autowired
    private TCourseOrderConfigMapper tCourseOrderConfigMapper;

    @Autowired
    private StudentClassMapper studentClassMapper;

    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Override
    public String updateBatch(TLabel tLabel ,List<TLabel> tLabels) {
        baseMapper.update(tLabel,new QueryWrapper<TLabel>().eq("org_id",tLabel.getOrgId()).eq("lable_type",tLabels.get(0).getLableType()));
        List<TLabel> updateList = tLabels.stream().filter(u-> !Objects.isNull(u.getId())).collect(Collectors.toList());
        updateList.stream().forEach(u->{
            //当前登录人id
            u.setUpdateUser(tLabel.getUpdateUser() );
            u.setUpdateDate(LocalDateTime.now());
            u.setIsDelete("0");
        });
        Boolean boo = this.updateBatchById(updateList);
        List<TLabel> insertList = tLabels.stream().filter(u-> Objects.isNull(u.getId())).collect(Collectors.toList());
        insertList.stream().forEach(u->{
            //机构id
            u.setOrgId(tLabel.getOrgId() );
            //当前登录人id
            u.setCreateUser(tLabel.getUpdateUser());
            u.setCreateDate(LocalDateTime.now());
            u.setIsDelete("0");
        });
        this.saveBatch(insertList);
        return "S200";
    }

    @Override
    public List<TLabel> classRoomList(TLabel tLabel) {
        List<TLabel> list = baseMapper.selectList(new QueryWrapper<TLabel>().eq("org_id",tLabel.getOrgId()).eq("is_delete","0").eq("lable_type",tLabel.getLableType()));
        BaseMapper base = tCourseOrderConfigMapper;
        String column = "class_room";
        String deleted = "is_delete";
        switch (tLabel.getLableType()){
            case "class_room" : {
                base = tCourseOrderConfigMapper;
                column = "class_room";
                deleted = "is_delete";
                break;
            }
            case "class_type" : {
                base = studentClassMapper;
                column = "class_type";
                deleted = "deleted";
                break;
            }
            case "pay_type" : {
                base = studentCourseMapper;
                column = "pay_type";
                deleted = "deleted";
                break;
            }
        }

        for(TLabel t : list){
            List<T> tList = base.selectList(new QueryWrapper<T>().eq(column, t.getId()).eq(deleted, "0"));
            if(!CollectionUtils.isEmpty(tList)){
                t.setUsed("1");
            }else{
                t.setUsed("0");
            }
        }
        return list;
    }

    @Override
    public List<TLabel> selById(Long id) {
        List<TLabel> label = baseMapper.selById(id);
        for (TLabel tLabel : label) {
            tLabel.setIsDelete("1");
            baseMapper.updateById(tLabel);
        }
        return label;
    }
}
